const app = getApp()  //获取app实例
 function wxRequest(config) {
    //返回的数据类型
    let dataType = config.dataType == null ? "json" : config.dataType;
    let responseType = config.responseType == null ? "text" : config.responseType;
    //服务器基地址
    const baseUrl = app.getConfig("baseUrl") //'http://localhost:8080'
    //超时
    let timeout = config.timeout == null ? 300000 : config.timeout;
    //目标地址，基地址+接口
    let url = baseUrl + config.url;
    //数据提交方式
    let method = config.method == null ? "GET" : config.method;
    //提交数据
    let data = config.data == null ? null : config.data
    //头信息
    let header = {
      // 默认值
      'content-type': 'application/json',
        'x-requested-with': 'XMLHttpRequest'
    }
    let sessionId = wx.getStorageSync('sessionId')
    if (sessionId) {
        header["cookie"] = sessionId
    }
    return new Promise(function (resolve, reject) {
        wx.request({
        url: url,
        data: data,
        //返回的数据类型（json）
        dataType: dataType,
        enableCache: false,
        enableHttp2: false,
        enableQuic: false,
        method: method,
        header: header,
        responseType: responseType,
        timeout: timeout,
        success: (res) => {
            console.log("requestData", res)
            if (res.cookies != null && res.cookies.length != 0)
            wx.setStorageSync('sessionId', res.cookies[0])
            resolve(res)
        },
        fail: (res) => {
            console.log("requestException", res)
            reject(res)
        }
        })
    })
}

module .exports = {
    wxRequest: wxRequest
    }